package spolipar.bitcoin.bot.data.formatter;
import java.sql.Date;
import java.util.Arrays;

import spolipar.bitcoin.bot.data.DeepData;


public class DeepStringFormatter {
	
	public static String format(DeepData deep) {
		StringBuilder deepString = new StringBuilder("Deep: ");
		deepString.append(new Date(deep.getTimestamp()) + "\n");
		
		double[][] bids = deep.getBids();
		double[][] asks = deep.getAsks();
		int maxBid = 0;
		int levels = Math.min(20, Math.min(bids.length, asks.length));
		for (int i = 0; i < levels; i++) {
			maxBid = Math.max(doPriceLine(bids, i).length(), maxBid );
		}
		maxBid +=4; 
		
		char[] gap = new char[maxBid];
		Arrays.fill(gap, 0, gap.length -1, ' ');
		
		deepString.append("bid:               ask:" + "\n");

		for (int i = 0; i < levels; i++) {
			String bidLine = doPriceLine(bids,i);
			deepString.append( bidLine);
			deepString.append( gap, 0, maxBid - bidLine.length());
			deepString.append( doPriceLine(asks,i));
			deepString.append( "\n");
		}
		return deepString.toString();
	}
	
	private static String doPriceLine(double data[][], int i){
		String price = String.valueOf(data[i][0]);
		String vol = String.valueOf(data[i][1]);
		
		String separator = "   ";
		return price.length() < 6 ? price + "0" + separator + vol : price + separator + vol;
	}
	
	
	/** HTML **/
	public static String formatHTML(DeepData deep) {
		if(deep == null) {
			return deep +"";
		}
		
		StringBuilder deepString = new StringBuilder("Deep: ");
		deepString.append(new Date(deep.getTimestamp()) + "\n");
		
		double[][] bids = deep.getBids();
		double[][] asks = deep.getAsks();
		int levels = Math.min(20, Math.min(bids.length, asks.length));
		
		
		
		deepString.append("<table>");
		//------------ Header ----------------------
		deepString.append("<thead>");
		
		deepString.append("<tr>" + doHTMLCell("Bid", 2) +  doHTMLCell("Ask", 2) + "</tr>\n"); 
		
		deepString.append("</thead>");
		
		// ------- Body ---------------------
		deepString.append("<tbody>");
		for (int i = 0; i < levels; i++) {
			deepString.append( doPriceRow(bids, asks, i));
		}
		deepString.append("</tbody>");
		
		deepString.append("</table>");
		return deepString.toString();
	}
	
	private static String doPriceRow(double bid[][], double ask[][], int i){
		StringBuilder b = new StringBuilder();
		
		b.append("<tr>");
		
		String bidPrice = String.valueOf(bid[i][0]);
		bidPrice = bidPrice.length() < 6 ? bidPrice + "0" : bidPrice;
		
		String askPrice = String.valueOf(ask[i][0]);
		askPrice = askPrice.length() < 6 ? askPrice + "0" : askPrice;
		
		b.append(doPriceHTMLCell(bidPrice)).append(doHTMLCell(bid[i][1]));
		b.append(doPriceHTMLCell(askPrice)).append(doHTMLCell(ask[i][1]));
		
		b.append("</tr>\n");
		return b.toString();
	}
	
	private static String doHTMLCell(double value) {
		return "<td>" + value + "</td>";
	}
	
	private static String doHTMLCell(String value) {
		return "<td>" + value + "</td>";
	}
	
	private static String doPriceHTMLCell(String value) {
		return "<td><a href=\"setPrice:" + value + "\">"  + value + "</a></td>";
	}
	
	private static String doHTMLCell(String value, int colspan) {
		//"<td colspan="3">Cell 1</td>"
		return "<td colspan=\"" + colspan + "\">"  + value + "</td>";
	}
	
}
